package wafteam.transData;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;

public class TransData {
	//private HandleJSON obj;
	static String serverName = "http://androidteam.esy.es/";
	static String apiName = "";
	public void getData(String apiName, Array comicId){
		//obj = new HandleJSON(finalUrl);
	    //obj.fetchJSON();
	}
	
	public String fetchJSON(){
		String data = null;
		String id = "";
	      
	     try {
	        URL url = new URL(serverName);
	        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	        conn.setReadTimeout(10000 /* milliseconds */);
	        conn.setConnectTimeout(15000 /* milliseconds */);
	        conn.setRequestMethod("GET");
	        conn.setDoInput(true);
	        // Starts the query
	        conn.connect();
	        InputStream stream = conn.getInputStream();
	
	        data = convertStreamToString(stream);
	        JSONObject json = new JSONObject(data);
	        id = json.getString("id");
	        //readAndParseJSON(data);
	        stream.close();
	
	     } catch (Exception e) {
	        //e.printStackTrace();
	        return "Không thể kết nối mạng!";
	     }

		return id;
	}
	
	private String readAndParseJSON(String data){
		return data;
	}
	static String convertStreamToString(java.io.InputStream is) {
	    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
	    return s.hasNext() ? s.next() : "";
	}
}
